﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;
using StateGrid;
using System.Xml;
using StateGrid.GlobalVariables;

namespace StateGrid.HYBZ
{
    /// <summary>
    ///HYBZ_DAO 的摘要说明
    /// </summary>
    public class HYBZ_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();
        
        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有行业标准分类
        /// </summary>
        public IList<HYBZ_FL> GetAllHYBZ_FL()
        {
            return sqlMapper.QueryForList<HYBZ_FL>("SelectAllHYBZ_FL", null);
        }

        /// <summary>
        /// 获取所有行业标准篇名
        /// </summary>
        public IList<HYBZ_PM> GetAllHYBZ_PM()
        {
            return sqlMapper.QueryForList<HYBZ_PM>("SelectAllHYBZ_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public HYBZ_FL GetHYBZ_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<HYBZ_FL>("SelectHYBZ_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按照分类编码名称查找分类信息
        /// </summary>
        /// <param name="flmc">分类名称</param>
        /// <returns></returns>
        public HYBZ_FL GetHYBZ_FLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<HYBZ_FL>("SelectHYBZ_FLByFlmc", flmc);
        }

        /// <summary>
        /// 获取指定分类编码、发布年份的篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="fbnf">发布年份</param>
        public int GetCountSomeHYBZ_PM(String flbm, String fbnf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String bzwh = "%";

            if (null != fbnf && fbnf.Length == 4)
            {
                bzwh = bzwh + fbnf;
            }

            flbm = flbm + "%";

            para.Add("bzwh", bzwh);
            para.Add("flbm", flbm);
            return sqlMapper.QueryForObject<int>("CountSomeHYBZ_PM", para);
        }

        /// <summary>
        /// 获取指定分类编码、发布年份的篇名
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="fbnf">发布年份</param>
        /// <param name="start">起始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<HYBZ_PM> GetSomeHYBZ_PM(String flbm, String fbnf, int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            String bzwh = "%";

            if (null != fbnf && fbnf.Length == 4)
            {
                bzwh = bzwh + fbnf;
            }

            flbm = flbm + "%";

            para.Add("bzwh", bzwh);
            para.Add("flbm", flbm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<HYBZ_PM>("SelectSomeHYBZ_PM", para);
        }

        /// <summary>
        /// 获取指定标准文号的篇名信息
        /// </summary>
        /// <param name="bzwh">标准文号</param>
        /// <returns></returns>
        public HYBZ_PM GetHYBZ_PMByBzwh(String bzwh)
        {
            return sqlMapper.QueryForObject<HYBZ_PM>("SelectHYBZ_PMByBzwh", bzwh);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="hybz_fl">分类信息</param>
        /// <returns></returns>
        public Boolean InsertHYBZ_FL(HYBZ_FL hybz_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertHYBZ_FL", hybz_fl);
            }
            catch (Exception)
            {
                flag = false;
            }

            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="hybz_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertHYBZ_PM(HYBZ_PM hybz_pm)
        {
            Boolean flag = true;
            OperationLogItem operationLog = this.GetOperationLogItem(hybz_pm, "INSERT", null);
            try
            {
                sqlMapper.Insert("InsertHYBZ_PM", hybz_pm);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="hybz_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateHYBZ_FL(HYBZ_FL hybz_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateHYBZ_FL", hybz_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="hybz_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateHYBZ_PM(HYBZ_PM hybz_pm, String uname)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateHYBZ_PM", hybz_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(hybz_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 按分类编码删除分类信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <returns></returns>
        public Boolean DeleteHYBZ_FL(String flbm)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteHYBZ_FLByFlbm", flbm);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按标准文号删除篇名信息
        /// </summary>
        /// <param name="bzwh">标准文号</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>

        public int DeleteHYBZ_PM(String bzwh, String uname)
        {
            int result = 0;

            HYBZ_PM hybzPM = this.GetHYBZ_PMByBzwh(bzwh);
            if (hybzPM == null) return -1;

            OperationLogItem operationLog = null;

            String flbm = hybzPM.flbm;

            //切分标准文号
            String[] str = bzwh.Split(' ');
            if (str.Length < 1) return -1;
            String fbnf = str[str.Length-1].Split('-')[1];
            //流水号？
            String lsh = str[str.Length-1].Split('-')[0];

            //删除指定篇名及文件
            if (null != hybzPM.wjm && !"".Equals(hybzPM.wjm) && OperationFile.DeleteFile(hybzPM.wjm))
            {
                result = sqlMapper.Delete("DeleteHYBZ_PMByBzwh", bzwh);
            }
            else
            {
                //不存在文件时
                result = sqlMapper.Delete("DeleteHYBZ_PMByBzwh", bzwh);
            }

            operationLog = this.GetOperationLogItem(hybzPM, "DELETE", uname);
            if (null != operationLog)
            {
                this.operationLogDAO.insertLogItem(operationLog);
            }

            //更新替代关系

            /* 流水号暂时不考虑 
            ICollection<HYBZ_PM> list = this.GetSomeHYBZ_PM(str[0], str[1], 1, 10000);

            int ph = 1;
            String phString = null;
            if (null != list)
            {
                foreach (HYBZ_PM temp in list)
                {
                    phString = FormatPH(ph++);
                    //如果当前文件名与新文件名一样则不做处理
                    if (temp.wjm.Equals(wjmStr + phString))
                    {
                        continue;
                    }
                    //如果当前文件名与新文件名不一致,则更新文件名及篇名信息
                    else
                    {
                        changeHYBZPMWjm(temp.wjm, wjmStr + phString);
                    }
                }
            }
            */
            return result;
        }

        /// <summary>
        /// 修改已有的文件名。
        /// </summary>
        /// <param name="oldWjm">旧文件名</param>
        /// <param name="newWjm">新文件名</param>
        /// <returns>是否修改成功</returns>
        public bool changeHYBZPMWjm(String oldWjm, String newWjm)
        {
            if (null == oldWjm || null == newWjm)
            {
                return false;
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldwjm", oldWjm);
            para.Add("newwjm", newWjm);

            bool result = true;
            try
            {
                sqlMapper.Update("ChangeHYBZ_PM_wjm", para);
                //修改文件名
                OperationFile.ReNameFile(oldWjm, newWjm);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>4位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            //长度为2
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            return phString + ".PDF";
        }

        /// <summary>根据标准文号获得同类的旧标准</summary>
        /// <param name="bzwh">标准文号</param>
        public HYBZ_PM GetAlikeHYBZ_PM(String bzwh)
        {
            bzwh = bzwh + "%";
            return sqlMapper.QueryForObject<HYBZ_PM>("SelectAlikeHYBZ_PM", bzwh);
        }

        /// <summary>
        /// 根据分类编码、发布年份获取最大文件的篇名信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="fbnf">发布年份</param>
        /// <returns></returns>
        public HYBZ_PM getMaxHYBZ_PM(String flbm, String fbnf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String bzwh = "%";

            if (null != fbnf && fbnf.Length == 4)
            {
                bzwh = bzwh + fbnf;
            }

            flbm = flbm + "%";

            para.Add("bzwh", bzwh);
            para.Add("flbm", flbm);
            return sqlMapper.QueryForObject<HYBZ_PM>("SelectMaxHYBZ_PM", para);
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="zwqkPM">中文篇名信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(HYBZ_PM hybzPM, String opType, String uname)
        {
            if (null == hybzPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "HYBZ_PM";
            //全文页数
            operationLogItem.opPageCount = hybzPM.ys;

            operationLogItem.opObjectID = "行业标准";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "行业标准";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + 
                hybzPM.flbm + "','" + hybzPM.bzwh + "','" + 
                hybzPM.wjm + "','" + hybzPM.bbxz + "','" + 
                hybzPM.zwmc + "','" + hybzPM.icsfl + "','" + 
                hybzPM.gbfl + "','" + hybzPM.wwmc + "','" + 
                hybzPM.fbrq + "','" + hybzPM.ssrq + "','" + 
                hybzPM.cygx + "','" + hybzPM.dtbz + "','" + 
                hybzPM.bdtbz + "','" + hybzPM.ys + "','" + 
                hybzPM.zf + "','" + hybzPM.zfnf + "','" + 
                hybzPM.zwztc + "','" + hybzPM.wwztc + "','" + 
                hybzPM.qcdw + "','" + hybzPM.fbdw + "','" + 
                hybzPM.gkdw + "','" + hybzPM.bz + "','" + 
                hybzPM.bzdhhy + "','" + hybzPM.ly + "','" + 
                hybzPM.qrnd + "','" + hybzPM.gxrq + "','" + 
                hybzPM.dlr + 
                "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }

        public HYBZ_DAO()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    }
}